﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using umbraco.DataLayer;
using umbraco.BusinessLogic;
using umbraco.editorControls;


namespace TwentyTree4Umbraco.Datatypes
{
    public class VideoUploaderPrevalueEditor : System.Web.UI.WebControls.PlaceHolder, umbraco.interfaces.IDataPrevalue
    {

        private umbraco.cms.businesslogic.datatype.BaseDataType _datatype;
        private TextBox _txtLimit;


        public VideoUploaderPrevalueEditor(umbraco.cms.businesslogic.datatype.BaseDataType DataType)
        {

            _datatype = DataType;
            setupChildControls();
        }

        private void setupChildControls()
        {

            _txtLimit = new TextBox();
            _txtLimit.ID = "txtAlbumID";
            _txtLimit.CssClass = "umbEditorTextField";


            Controls.Add(_txtLimit);
        }



        public Control Editor
        {
            get
            {
                return this;
            }
        }

 

        protected override void OnLoad(EventArgs e)
        {

            base.OnLoad(e);

            if (!Page.IsPostBack)
            {


                if (Configuration.Length > 0)
                {


                    _txtLimit.Text = Configuration;


                }


            }

        }

 

        public void Save()
        {

            _datatype.DBType = (umbraco.cms.businesslogic.datatype.DBTypes)Enum.Parse(typeof(umbraco.cms.businesslogic.datatype.DBTypes), DBTypes.Ntext.ToString(), true);


            string data = _txtLimit.Text;

 

            SqlHelper.ExecuteNonQuery("delete from cmsDataTypePreValues where datatypenodeid = @dtdefid", 

                    SqlHelper.CreateParameter("@dtdefid", _datatype.DataTypeDefinitionId));

            SqlHelper.ExecuteNonQuery("insert into cmsDataTypePreValues (datatypenodeid,[value],sortorder,alias) values (@dtdefid,@value,0,'')", 

                    SqlHelper.CreateParameter("@dtdefid", _datatype.DataTypeDefinitionId), SqlHelper.CreateParameter("@value", data));

 

        }

 

        protected override void Render(HtmlTextWriter writer)

        {

            writer.WriteLine("<table>");

            writer.Write("<tr><th>Album (or channel) ID:</th><td>");

            _txtLimit.RenderControl(writer);

            writer.Write("</td></tr>");

            writer.Write("</table>");

        }

 

        public string Configuration
        {

            get
            {

                object conf =

                   SqlHelper.ExecuteScalar<object>("select value from cmsDataTypePreValues where datatypenodeid = @datatypenodeid",

                                           SqlHelper.CreateParameter("@datatypenodeid", _datatype.DataTypeDefinitionId));

 

                if (conf != null)

                    return conf.ToString();

                else

                    return "";

            }

        }

 



 

        public static ISqlHelper SqlHelper
        {
            get
            {
                return Application.SqlHelper;
            }
        }
    }
}
